package operator.function.do_;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import util.Printer;
import util.RxJavaHelper;
import util.ThreadHelper;

class DoOnDisposeOperator {

    public static void main(String[] args) {
        doOnDispose();
    }

    static void doOnDispose() {
        Observable.interval(1, TimeUnit.SECONDS)
                .doOnDispose(new Action() {
                    @Override
                    public void run() throws Exception {
                        Printer.print("doOnDispose");
                    }
                })
                .subscribe(new Observer<Long>() {

                    Disposable disposable;

                    @Override
                    public void onSubscribe(Disposable disposable) {
                        Printer.print("doOnDispose onSubscribe");
                        this.disposable = disposable;
                    }

                    @Override
                    public void onNext(Long aLong) {
                        Printer.print("doOnDispose onNext aLong = " + aLong);
                        if(aLong == 3){
                            Printer.print("doOnDispose onNext中解除订阅");
                            disposable.dispose();
                        }
                    }

                    @Override
                    public void onError(Throwable throwable) {

                    }

                    @Override
                    public void onComplete() {
                        Printer.print("doOnDispose onComplete");
                    }
                });

        ThreadHelper.sleep();
    }


}
